namespace ClassyBlog.Infrastructure
{
    using System;
    using System.Web.Mvc;

    public abstract class ThemeableBuildManagerViewEngine :
        ThemeableVirtualPathProviderViewEngine
    {
        protected ThemeableBuildManagerViewEngine(
            IBuildManager buildManager, IViewPageActivator viewPageActivator)
        {
            if (buildManager == null)
            {
                throw new ArgumentNullException("buildManager");
            }

            if (viewPageActivator == null)
            {
                throw new ArgumentNullException("viewPageActivator");
            }

            BuildManager = buildManager;
            ViewPageActivator = viewPageActivator;
        }

        protected IBuildManager BuildManager { get; private set; }

        protected IViewPageActivator ViewPageActivator { get; private set; }

        protected override sealed bool FileExists(
            ControllerContext controllerContext, string virtualPath)
        {
            return BuildManager.FileExists(virtualPath);
        }

        protected override sealed bool? IsValidPath(
            ControllerContext controllerContext, string virtualPath)
        {
            var compiledType = BuildManager.GetCompiledType(virtualPath);

            return compiledType == null ?
                   (bool?)null :
                   IsValidCompiledType(
                   controllerContext, virtualPath, compiledType);
        }

        protected abstract bool IsValidCompiledType(
            ControllerContext controllerContext,
            string virtualPath,
            Type compiledType);
    }
}